﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using InstrumentCheck.Module;
using InstrumentCheck.BLL;

namespace InstrumentCheck.WebAppUI.UserControl
{
    public partial class UCSelectAssetNO : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            string strTableName = txtQueryTableName.Text;
            List<Item> list = Asset_bll.GetQueryAssetInfo(strTableName, "ast_no", txtAssetNO.Text);
            Session["list"] = list;

            if(list == null)
                lblNodata.Text = "找不到任何資料！";

            if (list == null && (strTableName.ToUpper() == "TBL_MASTER" || strTableName.ToUpper() == "TBL_CONTROL"))
            {                
                //重新找一次，如果存在基本资料档则提示已经存在，否则就不用修改label的信息了
                if (Asset_bll.ExistInItem(txtAssetNO.Text.Trim()))
                    lblNodata.Text = "此編號已存在於校驗系統中";
            }
            BindGridView(list);
        }

        private void BindGridView(List<Item> list)
        {
            gvAssetNO.DataSource = list;
            gvAssetNO.DataBind();
            gvAssetNO.Visible = true;

            if (list == null)
            {                
                gvAssetNO.Visible = false;
            }
            else
            {
                lblNodata.Text = "";
                gvAssetNO.Visible = true;
            }
        }

        protected void gvAssetNO_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Item item = e.Row.DataItem as Item;
                e.Row.Cells[1].Text = item.Keeper.DeptNO;
                e.Row.Cells[2].Text = item.Keeper.EmpChineseName;
                e.Row.Cells[7].Text = item.Keeper.EmpNO;

                string strValue = Decode(e.Row.Cells[0].Text) + "','" + Decode(item.Keeper.DeptNO) + "','";
                strValue += Decode(item.Keeper.EmpChineseName) + "','" + Decode(e.Row.Cells[3].Text) + "','";
                strValue += Decode(e.Row.Cells[4].Text) + "','" + Decode(e.Row.Cells[5].Text) + "','";
                strValue += Decode(e.Row.Cells[6].Text) + "','" + Decode(item.Keeper.EmpNO);

                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#E0E0E0'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                e.Row.Attributes["style"] = "Cursor:hand";
                e.Row.Attributes.Add("onclick", "returnValue('" + strValue + "')");
            }

            if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
                e.Row.Cells[7].Visible = false;
        }

        private string Decode(string str)
        {
            return System.Web.HttpUtility.HtmlDecode(str);
        }

        protected void gvAssetNO_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvAssetNO.PageIndex = e.NewPageIndex;
            List<Item> list = Session["list"] as List<Item>;
            BindGridView(list);
        }
    }
}